package com.taotao.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import Utils.ItemCatResult;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.pojo.ItemCat;
import com.taotao.service.ItemCatService;

@Controller
public class ItemCatController {
	
	private Logger LOGGER = LoggerFactory.getLogger(ItemCatController.class);
	private ObjectMapper mapper;
	@Autowired
	private ItemCatService service;
	
	@RequestMapping(value="item/cat", method=RequestMethod.GET)
	public ResponseEntity<List<ItemCat>> getItemCatByParentId(
			@RequestParam(value="id",defaultValue="0")Long id){
		try {
			if(LOGGER.isDebugEnabled())
			    LOGGER.debug("开始");
			List<ItemCat> list = service.getItemCatByParentId(id);
			if(list!=null&&list.size()>0){
				return ResponseEntity.ok(list);
			}else{
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
		
	}
	
	@RequestMapping(value="item/cat/all", method=RequestMethod.GET,produces=MediaType.TEXT_HTML_VALUE)
	public ResponseEntity<String> getItemCatAll(){
		try {
			if(LOGGER.isDebugEnabled())
			    LOGGER.debug("开始");
			 
			ItemCatResult result = service.queryAllToTree();
			String json = mapper.writeValueAsString(result);
			return ResponseEntity.ok(json);
			 
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
		
	}
//	@RequestMapping(value="item/cat/{id}",method=RequestMethod.GET)
//	public ResponseEntity<ItemCat> getItemCatById(
//			                   @PathVariable("id")Long id){
//		try {
//			ItemCat itemCat = service.queryById(id);
//			return ResponseEntity.status(HttpStatus.OK).body(itemCat);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//	}
}
